import 'package:flutter/material.dart';

import '../common/index.dart';
import '../tools/index.dart';
import '../vm/index.dart';

class DemoV extends StatefulWidget {
  const DemoV({super.key});
  @override
  State<DemoV> createState() => _DemoVState();
}

class _DemoVState extends State<DemoV> {
  // --------- 公开属性  --------- //

  // --------- 私有属性  --------- //
  // 获取主题管理器实例
  final ThemeManager _themeManager = ThemeManager();
  final DemoVM _vm = DemoVM();
  // --------- 生命周期  --------- //

  // --------- 构建UI  --------- //
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DemoV'),
        flexibleSpace: Image.asset(context.themeImages.navBg,
            fit: BoxFit.fill, height: SystemScreen.getNavHeight(context)),
        actions: [
          // 主题切换按钮
          IconButton(
            icon: Icon(
              _themeManager.isDarkMode ? Icons.light_mode : Icons.dark_mode,
            ),
            onPressed: () {
              _themeManager.toggleTheme();
              // 强制刷新页面
              setState(() {});
            },
            tooltip: '切换主题',
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: tapIncrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              "${_vm.counter}",
              style: Theme.of(context).textTheme.bodyLarge,
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: tapResetCounter,
              child: const Text('Reset Counter'),
            ),
          ],
        ),
      ),
    );
  }
  // --------- 数据处理  --------- //

  // --------- 触发事件  --------- //
  tapIncrementCounter() {
    _vm.incrementCounter();
    setState(() {});
  }

  tapResetCounter() {
    _vm.resetCounter();
    setState(() {});
  }
}
